{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee1b4313-05e9-4617-bf10-856395f7dad6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c6b7d86-2e57-44fe-b8c2-2d47f94d1f51",
   "metadata": {},
   "source": [
    "# 数组处理程序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd3ce46-2fc7-4cc3-9a36-6df7885db123",
   "metadata": {
    "tags": [],
    "toc-hr-collapsed": true
   },
   "source": [
    "## 基本操作\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "copyto(dst, src[, casting, where])|将值从一个数组复制到另一个数组，并根据需要进行广播。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51496857-1d9e-482f-81ca-a0afd360a792",
   "metadata": {},
   "source": [
    "### numpy.copyto(dst, src, casting='same_kind', where=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dbe37ae3-d2ee-4611-9c73-13459994b1cf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([4, 5, 6])\n",
    "B = [1, 2, 3]\n",
    "np.copyto(A, B)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e8fc5672-1b20-446c-acea-a14fb08419a4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "B = [[4, 5, 6], [7, 8, 9]]\n",
    "np.copyto(A, B)\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "799104f6-36f3-4efb-b99c-c6ce06ddd0f3",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 改变数组形状\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "reshape(a, newshape[, order])|在不更改数据的情况下为数组赋予新的形状。\n",
    "ravel(a[, order])|返回一个连续的扁平数组。\n",
    "ndarray.flat|数组上的一维迭代器。\n",
    "ndarray.flatten([order])|返回折叠成一维的数组副本。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f01f07a-605a-4aa8-845e-5e241ce106f9",
   "metadata": {},
   "source": [
    "### numpy.reshape(a, newshape, order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6ae00a2e-67a1-4b8f-bfa0-659212d9469c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(6).reshape((3, 2))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f587ef59-acd8-470f-9bae-91bc4e462ed8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, (2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a0cb3131-b2c3-4b59-a7ed-a60433a5667c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(np.ravel(a), (2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6fa46364-5a3f-421f-bdaa-d969e10046ad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 4, 3],\n",
       "       [2, 1, 5]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, (2, 3), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c3d9dff-e2ae-4c73-9ebf-aedaae6c0e10",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 4, 3],\n",
       "       [2, 1, 5]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(np.ravel(a, order='F'), (2, 3), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d11be1ad-61d2-48c8-8b55-6425fceeb1e1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,6]])\n",
    "np.reshape(a, 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e31f45b5-0a24-4832-b6f9-e2e68d85253f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 2, 5, 3, 6])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, 6, order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a38f3c6a-6649-4d33-b4aa-94710a524922",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, (3,-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6726be3b-e925-451c-b0f1-374d497d3925",
   "metadata": {},
   "source": [
    "### numpy.ravel(a, order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "92396218-b29c-4559-a47e-b93b12cea041",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "np.ravel(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ebf112da-c85a-49a5-80ac-0ee1b11c80e9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "430d72b9-5e5e-41a6-95f4-d810382107a0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 2, 5, 3, 6])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel(x, order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c9d51d4c-4164-4b00-ab19-103f89dc65e0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 2, 5, 3, 6])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel(x.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "34956d60-ecd7-4244-9d93-ca13e9c6ab9c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ravel(x.T, order='A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "05faad2b-96e1-47f1-86cb-c6e0aebeeb79",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(3)[::-1]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5822edd3-eef1-4f37-b852-1ecb6feb3065",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel(order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "19ed0448-a6db-41ca-9e4c-356b6397244b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel(order='K')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0f66e103-4e25-4d65-92aa-c09ca57af0ba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  2,  4],\n",
       "        [ 1,  3,  5]],\n",
       "\n",
       "       [[ 6,  8, 10],\n",
       "        [ 7,  9, 11]]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "363ac0e0-c5f6-46c2-b764-a02c50d5d885",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  1,  3,  5,  6,  8, 10,  7,  9, 11])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel(order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "28d413ad-fa8d-4916-93d5-c4b5174cf65d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel(order='K')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "146c4d31-a37f-4f3f-88b9-fd986f25bae8",
   "metadata": {
    "tags": []
   },
   "source": [
    "### ndarray.flat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "03d791a9-57e1-42d9-80d1-ef176ec23cb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(1, 7).reshape(2, 3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d752bc94-60c1-4af2-8280-069e67dd4bbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.flat[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "97158d4b-85b2-4354-93fb-ab94444a8441",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "93e467f8-7232-41ac-98c6-4a1e753b4388",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.T.flat[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2d214cf8-7994-4670-87c8-542af0cd8a57",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.flatiter"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x.flat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3e7653dc-7129-496a-9652-7ed85734ee05",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 3],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.flat = 3; x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "50d57ee8-b776-4ffe-8ca7-291c12d62242",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 3],\n",
       "       [3, 1, 3]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.flat[[1,4]] = 1; x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feaf74cc-1e97-4ca5-908f-10e631bafcbe",
   "metadata": {},
   "source": [
    "### ndarray.flatten(order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "98842b21-dda5-4c73-a558-e6fad3946452",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2], [3,4]])\n",
    "a.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "09a754e0-aa0e-41f3-b2d2-2fb256cefb73",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 2, 4])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten('F')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f6ec349-70a2-4b67-a9cb-cdb324d7df1c",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 类转置操作\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "moveaxis(a, source, destination)|将数组的轴移到新位置。\n",
    "rollaxis(a, axis[, start])|向后滚动指定的轴，直到其位于给定的位置。\n",
    "swapaxes(a, axis1, axis2)|互换数组的两个轴。\n",
    "ndarray.T|转置数组。\n",
    "transpose(a[, axes])|排列数组的尺寸。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82936f05-5895-4df7-8b72-6517e01b4d95",
   "metadata": {},
   "source": [
    "### numpy.moveaxis(a, source, destination)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "17ed5851-3498-4e26-bcce-0ba1e83036da",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5, 3)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.zeros((3, 4, 5))\n",
    "np.moveaxis(x, 0, -1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "828487fb-2a5a-41ec-8da9-ac05ff60cc33",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3, 4)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.moveaxis(x, -1, 0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9cb32078-d987-4eff-af5b-976263276bde",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 4, 3)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(x).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7d2c4f00-7a80-4747-abdc-257a312649c5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 4, 3)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.swapaxes(x, 0, -1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ab86b506-8319-47ce-8193-9d843c7759bd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 4, 3)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.moveaxis(x, [0, 1], [-1, -2]).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "230041e9-bfcf-46b0-b7c7-7c5bea68ccb8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 4, 3)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63e0a05e-b08d-4f66-b794-68211455ce68",
   "metadata": {},
   "source": [
    "### numpy.rollaxis(a, axis, start=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "70849c63-6cd1-4260-b9a8-b441f02ea16f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 6, 4, 5)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.ones((3,4,5,6))\n",
    "np.rollaxis(a, 3, 1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "05e88242-2278-430a-94c7-d0feeae553bb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3, 4, 6)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rollaxis(a, 2).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "35caa2bc-0d6b-43ac-88c9-f14e8a0904e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 5, 6, 4)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rollaxis(a, 1, 4).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "008f8c12-1d31-499d-9550-c463fb3080bc",
   "metadata": {},
   "source": [
    "### numpy.swapaxes(a, axis1, axis2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a6e815d5-6644-4d08-a272-45b1648148b1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1,2,3]])\n",
    "np.swapaxes(x,0,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "66ab0bbc-afcc-4721-99aa-85a124bf4297",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3]],\n",
       "\n",
       "       [[4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a04b1381-e6f3-47a4-957c-602f4d6a663b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 4],\n",
       "        [2, 6]],\n",
       "\n",
       "       [[1, 5],\n",
       "        [3, 7]]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.swapaxes(x,0,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bd11a48-f56e-4a85-9f11-7d9d2cb57ab7",
   "metadata": {},
   "source": [
    "### ndarray.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ea5d0105-fa61-43c7-9963-813ea34b4c9f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6409c836-6735-47a0-b7d1-04af71a74ed7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [2, 4]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "57499b23-e9a3-4bd7-820a-9b2293b90bf7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "cca4d29d-94d9-430a-9c7a-ca59e59daec1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62c7cf6b-a6bb-4e2f-b719-f2dc5176cf68",
   "metadata": {},
   "source": [
    "### numpy.transpose(a, axes=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "99bda2d6-f7e0-4dcf-8e56-9e69ea3d427a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "315522ea-2535-4e1f-a56a-45551f0bd559",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [2, 4]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "25fe45f3-73e5-4613-9727-d1517219285c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "99bf1be6-c858-4a51-bce2-b4e6b6c3251e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "863dfa24-5b93-4509-b184-ec76aa557086",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1, 3)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.ones((1, 2, 3))\n",
    "np.transpose(a, (1, 0, 2)).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "8d9e0610-cb17-47b2-8311-d380b69973ca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 4, 3, 2)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.ones((2, 3, 4, 5))\n",
    "np.transpose(a).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55be42ec-cb12-4707-8d2c-d65c5af065af",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 更改维度数\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "atleast_1d(*arys)|将输入转换为至少一维的数组。\n",
    "atleast_2d(*arys)|将输入视为至少具有二维的数组。\n",
    "atleast_3d(*arys)|以至少三个维度的数组形式查看输入。\n",
    "broadcast|产生模仿广播的对象。\n",
    "broadcast_to(array, shape[, subok])|将数组广播为新形状。\n",
    "broadcast_arrays(*args, **kwargs)|互相广播任意数量的阵列。\n",
    "expand_dims(a, axis)|扩展数组的形状。\n",
    "squeeze(a[, axis])|从数组形状中删除一维条目。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05e0629c-e6bd-48f8-91ff-2ff3441c07c7",
   "metadata": {},
   "source": [
    "### numpy.atleast_1d(*arys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a0e74512-0783-427d-a9be-72c0a7e0e938",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_1d(1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "06b84553-f316-41b6-88b9-d87f6b59780b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1., 2.],\n",
       "       [3., 4., 5.],\n",
       "       [6., 7., 8.]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(9.0).reshape(3,3)\n",
    "np.atleast_1d(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "66f968fe-ee16-4f57-81cb-84ae688dd4c3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_1d(x) is x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "5bc38430-ad60-4cbf-90c3-a53aaa3eaec6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([1]), array([3, 4])]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_1d(1, [3, 4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f8d0045-19e0-491c-abf9-2dc396344947",
   "metadata": {},
   "source": [
    "### numpy.atleast_2d(*arys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "184b642f-1603-41cf-8999-e33f881ca942",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_2d(3.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "55bf91c4-c00a-4e33-a564-da3440fb69b1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1., 2.]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(3.0)\n",
    "np.atleast_2d(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8de748b5-e5fe-4767-a844-014a1cb6c628",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_2d(x).base is x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "85f7ccfc-8233-4514-be9d-242fee9677d8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1]]), array([[1, 2]]), array([[1, 2]])]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_2d(1, [1, 2], [[1, 2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "7a54d4bc-73e3-4fb0-9ff6-7f7e2680d2c5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 3, 1)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(12.0).reshape(4,3)\n",
    "np.atleast_3d(x).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "bbccedda-9c5d-4583-90eb-75308e7849c0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.atleast_3d(x).base is x.base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "60a0eefb-775e-499f-8a7b-90ca5c2923a9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1]\n",
      "  [2]]] (1, 2, 1)\n",
      "[[[1]\n",
      "  [2]]] (1, 2, 1)\n",
      "[[[1 2]]] (1, 1, 2)\n"
     ]
    }
   ],
   "source": [
    "for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]):\n",
    "    print(arr, arr.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d9e8e1c-ea22-41ae-94ef-9c274a1012ba",
   "metadata": {},
   "source": [
    "### class numpy.broadcast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "e3014fed-757a-4d2a-aff2-4384c114054f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5., 6., 7.],\n",
       "       [6., 7., 8.],\n",
       "       [7., 8., 9.]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1], [2], [3]])\n",
    "y = np.array([4, 5, 6])\n",
    "b = np.broadcast(x, y)\n",
    "out = np.empty(b.shape)\n",
    "out.flat = [u+v for (u,v) in b]\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "21dbf75f-1db6-402f-8d06-62aad313718e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 7],\n",
       "       [6, 7, 8],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "752fb68b-a352-434a-9874-709cd9c239e1",
   "metadata": {},
   "source": [
    "### numpy.broadcast_to(array, shape, subok=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "adeb9eb9-7dd7-435b-b3f9-bf180424338b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1, 2, 3])\n",
    "np.broadcast_to(x, (3, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "545444d0-2745-4777-86f3-0f34b582054d",
   "metadata": {},
   "source": [
    "### numpy.broadcast_arrays(*args, subok=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "952930bb-08e5-48be-9e6b-62f0ed8d957c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3],\n",
       "        [1, 2, 3]]),\n",
       " array([[4, 4, 4],\n",
       "        [5, 5, 5]])]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1,2,3]])\n",
    "y = np.array([[4],[5]])\n",
    "np.broadcast_arrays(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "a047eb8a-e563-4423-9d0f-e85b354c7a1f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3],\n",
       "        [1, 2, 3]]),\n",
       " array([[4, 4, 4],\n",
       "        [5, 5, 5]])]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[np.array(a) for a in np.broadcast_arrays(x, y)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2288ddbe-79bb-4ec5-96bd-5a3c63859ddb",
   "metadata": {},
   "source": [
    "### numpy.expand_dims(a, axis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "e6791c2a-1a16-4046-99b7-4fc7f54ed97d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2,)"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1, 2])\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "ab578c29-315c-4a39-906d-2a4a13c8cf22",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2]]), (1, 2))"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.expand_dims(x, axis=0)\n",
    "y, y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "7c67def7-7ff4-4823-8911-c3f237c3f0c3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1],\n",
       "        [2]]),\n",
       " (2, 1))"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.expand_dims(x, axis=1)\n",
    "y, y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "e5811b48-5590-45a9-8ac6-cc1fdd4e88c1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2]]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.expand_dims(x, axis=(0, 1))\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "a5cfbdb1-57d7-4226-90b1-bcd30f1e68d4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1],\n",
       "        [2]]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.expand_dims(x, axis=(2, 0))\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "9a73f8eb-c667-4762-9db5-b62ff20821d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.newaxis is None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efa52ced-f232-4449-9a6a-5406d261bc08",
   "metadata": {},
   "source": [
    "### numpy.squeeze(a, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "fd04bed2-5ae5-436f-a043-c2507f251e5d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 3, 1)"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[[0], [1], [2]]])\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "674560ed-a09b-44f4-9097-453d364ca60a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(x).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "0c2aab04-4c02-44ea-b3dc-94a33ea0378f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 1)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(x, axis=0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "0c844853-16f8-47dd-bc8a-3d76680c2034",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 3)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(x, axis=2).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "170e4448-ad0d-4aad-927b-5ccb6cc0d8cd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1234]])\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "4d8ef771-13a8-4383-8b58-094859bc14a8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(1234)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "0400710c-fb90-4350-ab15-531f8414014d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(x).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "b1cf4331-3e74-48b7-8a89-ef2c89331f3a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1234"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(x)[()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67ac828a-22b0-45ce-af81-164be649b4af",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 改变数组的种类\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "asarray(a[, dtype, order])|将输入转换为数组。\n",
    "asanyarray(a[, dtype, order])|将输入转换为ndarray，但通过ndarray子类。\n",
    "asmatrix(data[, dtype])|将输入解释为矩阵。\n",
    "asfarray(a[, dtype])|返回转换为浮点类型的数组。\n",
    "asfortranarray(a[, dtype])|返回以Fortran顺序排列在内存中的数组（ndim> = 1）。\n",
    "ascontiguousarray(a[, dtype])|返回内存中的连续数组（ndim> = 1）（C顺序）。\n",
    "asarray_chkfinite(a[, dtype, order])|将输入转换为数组，检查NaN或Infs。\n",
    "asscalar(a)|将大小为1的数组转换为其等效的标量。\n",
    "require(a[, dtype, requirements])|返回提供的类型满足要求的ndarray。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d8484d8-0d76-4b3f-85f3-72f89a33a52f",
   "metadata": {},
   "source": [
    "### numpy.asarray(a, dtype=None, order=None, *, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "0e75d0ab-04c7-4d71-8899-dcc0ba984c63",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2]\n",
    "np.asarray(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "d980e15d-1d1f-4056-b15c-acd14a8c8ecb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2])\n",
    "np.asarray(a) is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "23e7808f-2ba3-43d9-9f57-3cdb7251b6f5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2], dtype=np.float32)\n",
    "np.shares_memory(np.asarray(a, dtype=np.float32), a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "a1bc89dd-4ee1-4092-977d-1d2d1bb75107",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shares_memory(np.asarray(a, dtype=np.float64), a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "c835dab5-2b9d-43ec-a9e0-574f9163bbe0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issubclass(np.recarray, np.ndarray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "3e6ae355-7885-4894-9606-b10c9e40a5be",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.recarray)\n",
    "np.asarray(a) is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "4c6510ba-277b-4a63-ad9f-e0aff8e62291",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asanyarray(a) is a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2073a8d1-a461-4d15-be48-a336979d16fc",
   "metadata": {},
   "source": [
    "### numpy.asanyarray(a, dtype=None, order=None, *, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "357640dc-48f4-4b8d-9208-fc89d7a26f1c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2]\n",
    "np.asanyarray(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "4c5f7c2a-bcae-477b-bac7-0d33004556fc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.recarray)\n",
    "np.asanyarray(a) is a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffa4ee35-5fc8-4298-a66d-8f45be0ef256",
   "metadata": {},
   "source": [
    "### numpy.asmatrix(data, dtype=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "ca0e43d6-aa4f-4a76-9205-6fe7ab81d2f7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[5, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1, 2], [3, 4]])\n",
    "m = np.asmatrix(x)\n",
    "x[0,0] = 5\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62d9278b-8d0f-4d8d-b0d6-4e94cbf34dbb",
   "metadata": {},
   "source": [
    "### numpy.asfortranarray(a, dtype=None, *, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "b040752b-c168-4599-adaf-74df2b033579",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.ones((2, 3), order='C')\n",
    "x.flags['C_CONTIGUOUS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "f3d44352-7676-406d-879a-f6f604093ded",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.asfortranarray(x)\n",
    "y.flags['F_CONTIGUOUS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "c4797567-ed91-4bc7-bcce-548acedad1ad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.may_share_memory(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "fa9cf13e-fb31-4e94-b51d-008a0514617f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.ones((2, 3), order='F')\n",
    "x.flags['F_CONTIGUOUS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "e5e11784-721d-4225-a048-f66966dede26",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.asfortranarray(x)\n",
    "x is y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95e365ce-90b4-4d47-a207-d7ec6b20d3ac",
   "metadata": {},
   "source": [
    "### numpy.ascontiguousarray(a, dtype=None, *, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "970ba870-9115-4513-87d8-5b80b4262094",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.ones((2, 3), order='F')\n",
    "x.flags['F_CONTIGUOUS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "ab53e3bf-b06e-4bdc-8ed4-f97bcc40db51",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.ascontiguousarray(x)\n",
    "y.flags['C_CONTIGUOUS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "7f5de813-7f39-4c43-a627-3acfebb2f25c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.may_share_memory(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "8b3d5147-60eb-46a0-9ff4-9316ce237406",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.ones((2, 3), order='C')\n",
    "x.flags['C_CONTIGUOUS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "94dc4e7b-d17e-4cb5-bf55-5c290b9cb974",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.ascontiguousarray(x)\n",
    "x is y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f72888a6-eb63-4aca-aef2-b29f7c808235",
   "metadata": {},
   "source": [
    "### numpy.asarray_chkfinite(a, dtype=None, order=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "827c28e9-094a-491b-8de6-074911fbb08b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2.])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2]\n",
    "np.asarray_chkfinite(a, dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "a726baf7-aa57-41e5-914c-73f91edb8e53",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ValueError\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, np.inf]\n",
    "try:\n",
    "    np.asarray_chkfinite(a)\n",
    "except ValueError:\n",
    "    print('ValueError')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faeeb37f-2a92-45fe-9b03-fc4c724d0ec6",
   "metadata": {},
   "source": [
    "### numpy.require(a, dtype=None, requirements=None, *, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "dbf6e5ed-be1c-4929-88e4-67f2ac716fd1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : True\n",
       "  F_CONTIGUOUS : False\n",
       "  OWNDATA : False\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(6).reshape(2,3)\n",
    "x.flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "a5bf08d0-bb8e-4fe7-84df-42b45f334cdb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : False\n",
       "  F_CONTIGUOUS : True\n",
       "  OWNDATA : True\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F'])\n",
    "y.flags"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97b680f4-bbb6-4c02-8bad-039784d5660c",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 组合数组\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "concatenate((a1, a2, …)|沿现有轴连接一系列数组。\n",
    "stack(arrays[, axis, out])|沿新轴连接一系列数组。\n",
    "column_stack(tup)|将一维数组作为列堆叠到二维数组中。\n",
    "dstack(tup)|沿深度方向（沿第三轴）按顺序堆叠数组。\n",
    "hstack(tup)|水平（按列）顺序堆叠数组。\n",
    "vstack(tup)|垂直（行）按顺序堆叠数组。\n",
    "block(arrays)|从块的嵌套列表中组装一个nd数组。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c12b5c8-a9c6-4f34-8966-4292cc47cd3d",
   "metadata": {},
   "source": [
    "### numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting=\"same_kind\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "61e71d9e-6aa0-4a37-98a3-538934cb346b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6]])\n",
    "np.concatenate((a, b), axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "6bc7d58d-4f65-4ec2-b6c8-a1b8960ff4c3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 5],\n",
       "       [3, 4, 6]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a, b.T), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "de91bf72-5722-4e17-ada1-96fec350c414",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a, b), axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "f75a4844-697e-490c-9c6e-086da8b4cdd1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(masked_array(data=[0, --, 2],\n",
       "              mask=[False,  True, False],\n",
       "        fill_value=999999),\n",
       " array([2, 3, 4]))"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.ma.arange(3)\n",
    "a[1] = np.ma.masked\n",
    "b = np.arange(2, 5)\n",
    "a, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "e8e1f347-bbf3-46fb-aaf1-93ebaa6bab83",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "masked_array(data=[0, 1, 2, 2, 3, 4],\n",
       "             mask=False,\n",
       "       fill_value=999999)"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([a, b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "556b1601-61ea-49b0-9480-28ebe6b645bc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "masked_array(data=[0, --, 2, 2, 3, 4],\n",
       "             mask=[False,  True, False, False, False, False],\n",
       "       fill_value=999999)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ma.concatenate([a, b])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53ebddb5-b44b-469c-a2cc-24ba0e75e611",
   "metadata": {},
   "source": [
    "### numpy.stack(arrays, axis=0, out=None, *, dtype=None, casting='same_kind')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "5d3943c5-8b9a-4aaf-b8ba-e1a46168fc56",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 3, 4)"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arrays = [np.random.randn(3, 4) for _ in range(10)]\n",
    "np.stack(arrays, axis=0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "9ce67793-be29-49d0-9861-ffe6060569dc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 10, 4)"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.stack(arrays, axis=1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "96992a89-585b-4de6-8704-9ae2040ff431",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 10)"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.stack(arrays, axis=2).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "a6a09097-f71b-4848-b44c-5d0407c94e88",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([4, 5, 6])\n",
    "np.stack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "58de9db1-94b3-4ee5-8cd5-1cbd70b6b9ca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.stack((a, b), axis=-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22d21d2e-d347-46eb-9ffe-3e80e461801f",
   "metadata": {},
   "source": [
    "### numpy.column_stack(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "82f1ad57-c148-40f5-a4b1-40e3680a4ccd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [2, 3],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array((1,2,3))\n",
    "b = np.array((2,3,4))\n",
    "np.column_stack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14ab2fc6-dce3-4f61-9856-0d8f9e3a66bc",
   "metadata": {},
   "source": [
    "### numpy.dstack(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "f5be6155-7ad4-44bf-8a8d-847db24c39de",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2],\n",
       "        [2, 3],\n",
       "        [3, 4]]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array((1,2,3))\n",
    "b = np.array((2,3,4))\n",
    "np.dstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "a5b205a9-bcc1-4fa6-9f53-247fd8130ecb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2]],\n",
       "\n",
       "       [[2, 3]],\n",
       "\n",
       "       [[3, 4]]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1],[2],[3]])\n",
    "b = np.array([[2],[3],[4]])\n",
    "np.dstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20da814a-3a5e-4672-8da2-0c4228f64417",
   "metadata": {},
   "source": [
    "### numpy.hstack(tup, *, dtype=None, casting='same_kind')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "89781ec2-999d-416a-9199-c98444451054",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array((1,2,3))\n",
    "b = np.array((4,5,6))\n",
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "78980c9a-69a5-4631-8acc-ed573951d544",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1],[2],[3]])\n",
    "b = np.array([[4],[5],[6]])\n",
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43f4a9d0-b4f3-4da9-8663-5f195d6e8572",
   "metadata": {},
   "source": [
    "### numpy.vstack(tup, *, dtype=None, casting='same_kind')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "35d42afe-8758-45d1-ad41-8329910ee538",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([4, 5, 6])\n",
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "6bc925a8-4a5f-49fa-9450-ac8e37b2c8ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6]])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1], [2], [3]])\n",
    "b = np.array([[4], [5], [6]])\n",
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc1491f1-2902-4865-8fba-889ed2fc1b71",
   "metadata": {},
   "source": [
    "### numpy.block(arrays)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "2056e646-f64b-405f-b35e-6725b64d29c0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2., 0., 0., 0., 0.],\n",
       "       [0., 2., 0., 0., 0.],\n",
       "       [1., 1., 3., 0., 0.],\n",
       "       [1., 1., 0., 3., 0.],\n",
       "       [1., 1., 0., 0., 3.]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.eye(2) * 2\n",
    "B = np.eye(3) * 3\n",
    "np.block([\n",
    "    [A,               np.zeros((2, 3))],\n",
    "    [np.ones((3, 2)), B               ]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "e7460165-51d8-4c99-b6df-26f95a17b99c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "7cedc031-a19b-4347-bd22-d84c17e30a8d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6, 10])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([4, 5, 6])\n",
    "np.block([a, b, 10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "ab4a5252-0ff2-44ff-9591-52026bcb5abc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 2, 2],\n",
       "       [1, 1, 2, 2]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.ones((2, 2), int)\n",
    "B = 2 * A\n",
    "np.block([A, B])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "a703a3fe-90ea-4c39-b622-917dcaf68d44",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([4, 5, 6])\n",
    "np.block([[a], [b]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "3c2c9479-2882-4f67-912b-f360862dfb7b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [1, 1],\n",
       "       [2, 2],\n",
       "       [2, 2]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.ones((2, 2), int)\n",
    "B = 2 * A\n",
    "np.block([[A], [B]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "720afac6-c2b0-4b19-a90d-891c7ca3c99a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array(0)\n",
    "b = np.array([1])\n",
    "np.block([a])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "6d43ebd9-0208-4a4d-966d-ab4e61b283df",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "8ef50070-0250-4d78-96b5-0b97106daae2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([[a]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "54c7cb70-f485-48d2-9344-aa0a2381e992",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1]])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([[b]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58fdb692-36bb-421a-ab6c-e3318ed516ee",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 拆分数组\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "split(ary, indices_or_sections[, axis])|将数组拆分为多个子数组，作为ary的视图。\n",
    "array_split(ary, indices_or_sections[, axis])|将一个数组拆分为多个子数组。\n",
    "dsplit(ary, indices_or_sections)|沿第3轴（深度）将数组拆分为多个子数组。\n",
    "hsplit(ary, indices_or_sections)|水平（按列）将一个数组拆分为多个子数组。\n",
    "vsplit(ary, indices_or_sections)|垂直（行）将数组拆分为多个子数组。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6751832-0e87-4974-81a7-0593fc44ceb8",
   "metadata": {},
   "source": [
    "### numpy.split(ary, indices_or_sections, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "3f122814-cb12-4728-86f0-10e09e08f72d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(9.0)\n",
    "np.split(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "4dba6755-e9c4-46ce-99a3-7fc0808d6b78",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2.]),\n",
       " array([3., 4.]),\n",
       " array([5.]),\n",
       " array([6., 7.]),\n",
       " array([], dtype=float64)]"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8.0)\n",
    "np.split(x, [3, 5, 6, 10])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5147bd48-997f-454e-a1b1-3efdfcc5d488",
   "metadata": {},
   "source": [
    "### numpy.array_split(ary, indices_or_sections, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "72cb3ed5-be73-4380-ac5e-5bcb343354e8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7.])]"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8.0)\n",
    "np.array_split(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "fb146f41-89c3-4d64-a332-56731a63224f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(9)\n",
    "np.array_split(x, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70997948-a0ef-494b-8a27-12b8cb694795",
   "metadata": {},
   "source": [
    "### numpy.dsplit(ary, indices_or_sections)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "5706cc5d-c49a-4dbf-9f06-c5b6bb4dc054",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.]],\n",
       "\n",
       "       [[ 8.,  9., 10., 11.],\n",
       "        [12., 13., 14., 15.]]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(16.0).reshape(2, 2, 4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "29ef0db6-117b-406c-9745-9e7506d4343a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[[ 0.,  1.],\n",
       "         [ 4.,  5.]],\n",
       " \n",
       "        [[ 8.,  9.],\n",
       "         [12., 13.]]]),\n",
       " array([[[ 2.,  3.],\n",
       "         [ 6.,  7.]],\n",
       " \n",
       "        [[10., 11.],\n",
       "         [14., 15.]]])]"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dsplit(x, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "a6c5c88f-1011-4590-8b08-a8db55c48be5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[[ 0.,  1.,  2.],\n",
       "         [ 4.,  5.,  6.]],\n",
       " \n",
       "        [[ 8.,  9., 10.],\n",
       "         [12., 13., 14.]]]),\n",
       " array([[[ 3.],\n",
       "         [ 7.]],\n",
       " \n",
       "        [[11.],\n",
       "         [15.]]]),\n",
       " array([], shape=(2, 2, 0), dtype=float64)]"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dsplit(x, np.array([3, 6]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ae08c85-e2b6-4e4b-afd0-c15215c552c5",
   "metadata": {},
   "source": [
    "### numpy.hsplit(ary, indices_or_sections)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "71dd5154-ee5e-4fa8-9839-b3fcdc3df0b9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  1.,  2.,  3.],\n",
       "       [ 4.,  5.,  6.,  7.],\n",
       "       [ 8.,  9., 10., 11.],\n",
       "       [12., 13., 14., 15.]])"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(16.0).reshape(4, 4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "f426c30b-c890-43dd-b3ff-5343a1f9d42f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0.,  1.],\n",
       "        [ 4.,  5.],\n",
       "        [ 8.,  9.],\n",
       "        [12., 13.]]),\n",
       " array([[ 2.,  3.],\n",
       "        [ 6.,  7.],\n",
       "        [10., 11.],\n",
       "        [14., 15.]])]"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(x, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "9e178e52-00fd-4698-a36f-590afad02c0b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0.,  1.,  2.],\n",
       "        [ 4.,  5.,  6.],\n",
       "        [ 8.,  9., 10.],\n",
       "        [12., 13., 14.]]),\n",
       " array([[ 3.],\n",
       "        [ 7.],\n",
       "        [11.],\n",
       "        [15.]]),\n",
       " array([], shape=(4, 0), dtype=float64)]"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(x, np.array([3, 6]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "b6e96300-8fba-4024-b9f6-a124ae8fad0b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0., 1.],\n",
       "        [2., 3.]],\n",
       "\n",
       "       [[4., 5.],\n",
       "        [6., 7.]]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8.0).reshape(2, 2, 2)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "c4937cf7-651b-4c02-922c-20f5ef646738",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[[0., 1.]],\n",
       " \n",
       "        [[4., 5.]]]),\n",
       " array([[[2., 3.]],\n",
       " \n",
       "        [[6., 7.]]])]"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(x, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "87743caf-4764-4eef-b6e1-b63e5962b3df",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0, 1, 2]), array([3, 4, 5])]"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([0, 1, 2, 3, 4, 5])\n",
    "np.hsplit(x, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4271e69-5fc9-41f0-859e-ce5743b7f8ff",
   "metadata": {},
   "source": [
    "### numpy.vsplit(ary, indices_or_sections)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "ab54c1b3-493c-4b10-93e2-4fde7371777d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  1.,  2.,  3.],\n",
       "       [ 4.,  5.,  6.,  7.],\n",
       "       [ 8.,  9., 10., 11.],\n",
       "       [12., 13., 14., 15.]])"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(16.0).reshape(4, 4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "0d967dcc-82e6-40d9-8846-b21773404f52",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0., 1., 2., 3.],\n",
       "        [4., 5., 6., 7.]]),\n",
       " array([[ 8.,  9., 10., 11.],\n",
       "        [12., 13., 14., 15.]])]"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(x, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "d4377107-e062-49e9-96b3-73dbd3174bd1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]]),\n",
       " array([[12., 13., 14., 15.]]),\n",
       " array([], shape=(0, 4), dtype=float64)]"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(x, np.array([3, 6]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "b8fc8b85-a54b-462e-b2bb-0bfc20b2eb21",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0., 1.],\n",
       "        [2., 3.]],\n",
       "\n",
       "       [[4., 5.],\n",
       "        [6., 7.]]])"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8.0).reshape(2, 2, 2)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5de611f3-6b21-457e-88e9-d68a53dbebc6",
   "metadata": {
    "tags": [],
    "toc-hr-collapsed": true
   },
   "source": [
    "## 平铺数组\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "tile(A, reps)|通过重复A代表次数来构造一个数组。\n",
    "repeat(a, repeats[, axis])|重复数组的元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e604ac5-c5cf-408d-9182-305ed51f7768",
   "metadata": {},
   "source": [
    "### numpy.tile(A, reps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "2d35c3cf-45b6-433d-8c4d-3ed02521f98a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 0, 1, 2])"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0, 1, 2])\n",
    "np.tile(a, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "68b7da9d-4c7c-4d0e-a175-6e1fef87797d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 0, 1, 2],\n",
       "       [0, 1, 2, 0, 1, 2]])"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(a, (2, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "ed570694-4b5c-48a5-8b5e-71d72fd335fa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1, 2, 0, 1, 2]],\n",
       "\n",
       "       [[0, 1, 2, 0, 1, 2]]])"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(a, (2, 1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "073f7de8-0464-4050-9a37-65c88704adaf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 1, 2],\n",
       "       [3, 4, 3, 4]])"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[1, 2], [3, 4]])\n",
    "np.tile(b, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "0e97a77c-9399-4461-87f5-1e3a5454481b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(b, (2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "ebda117f-7938-43d3-8e78-b9aaae0634d3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [1, 2, 3, 4],\n",
       "       [1, 2, 3, 4],\n",
       "       [1, 2, 3, 4]])"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([1,2,3,4])\n",
    "np.tile(c,(4,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "810ec495-d7c4-40bb-b833-246283a9488d",
   "metadata": {},
   "source": [
    "### numpy.repeat(a, repeats, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "62aa0211-4f3b-424e-b89b-b37888a5930b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 3, 3])"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "c8829351-c9c5-420a-9870-a15feab63dc5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 2, 2, 3, 3, 4, 4])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1,2],[3,4]])\n",
    "np.repeat(x, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "b872ca7f-5771-411d-8d62-1d73886ea3c1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 2, 2, 2],\n",
       "       [3, 3, 3, 4, 4, 4]])"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(x, 3, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "359d8da6-d901-41ef-889f-3da0f72f5df1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(x, [1, 2], axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4105fe53-0870-4379-bb77-05f417190a97",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 添加和删除元素\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "delete(arr, obj[, axis])|返回一个新的数组，该数组具有沿删除的轴的子数组。\n",
    "insert(arr, obj, values[, axis])|沿给定轴在给定索引之前插入值。\n",
    "append(arr, values[, axis])|将值附加到数组的末尾。\n",
    "resize(a, new_shape)|返回具有指定形状的新数组。\n",
    "trim_zeross(filt[, trim])|修剪一维数组或序列中的前导和/或尾随零。\n",
    "unique(ar[, return_index, return_inverse, …])|查找数组的唯一元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30ff5635-4642-435c-a699-83b10201e2ca",
   "metadata": {},
   "source": [
    "### numpy.delete(arr, obj, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "25f9f1e9-8ed6-405e-9424-c502ed8e3da5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "5a84c858-07a7-4411-8a6c-37b00975b836",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(arr, 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "dae6f160-2d26-462f-acc6-588bc2fe565c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4],\n",
       "       [ 6,  8],\n",
       "       [10, 12]])"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(arr, np.s_[::2], 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "764b73a2-6e70-43cf-9caf-6c36fad2f867",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(arr, [1,3,5], None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b366f05-fc00-40f7-8367-a73a3877efd1",
   "metadata": {},
   "source": [
    "### numpy.insert(arr, obj, values, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "fdbda953-ae14-49d2-a751-e72cd1523f61",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 1],\n",
       "        [2, 2],\n",
       "        [3, 3]]),\n",
       " array([1, 5, 1, 2, 2, 3, 3]))"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 1], [2, 2], [3, 3]])\n",
    "a, np.insert(a, 1, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "54045e0c-ad09-48fe-bdb9-20821d537603",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5, 1],\n",
       "       [2, 5, 2],\n",
       "       [3, 5, 3]])"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(a, 1, 5, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "35220885-7d5b-474d-8e59-9b55d2496ea5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(a, [1], [[1],[2],[3]], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "8739904d-3225-44f1-9b72-1be7350c0925",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1),\n",
    "    np.insert(a, [1], [[1],[2],[3]], axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "9d2f1e17-52f1-4994-97da-ad57caf71a90",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 1, 2, 2, 3, 3]), array([1, 1, 5, 6, 2, 2, 3, 3]))"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.flatten()\n",
    "b, np.insert(b, [2, 2], [5, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "974a0b8b-5d41-4839-aa30-9904f307d2ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 5, 2, 6, 2, 3, 3])"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(b, slice(2, 4), [5, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "a90a81da-8779-4653-91e4-08076c06a62f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 7, 0, 2, 2, 3, 3])"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(b, [2, 2], [7.13, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "0b3a929f-edca-4c02-9ea1-e9efda212e63",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0, 999,   1,   2, 999,   3],\n",
       "       [  4, 999,   5,   6, 999,   7]])"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8).reshape(2, 4)\n",
    "idx = (1, 3)\n",
    "np.insert(x, idx, 999, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6144edf6-0959-4924-afe9-c1ab94a8d0c2",
   "metadata": {},
   "source": [
    "### numpy.append(arr, values, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "cec3d70b-8173-49a8-b950-3720625aaf4c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "84dd1911-2b48-4d10-8c71-a530b87e40cb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a285c844-ed43-4c7d-b07a-ec1cbfd0be31",
   "metadata": {},
   "source": [
    "### numpy.resize(a, new_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "d37372ba-4efc-4d09-b0b4-7cd1b2233b22",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 0, 1]])"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array([[0,1],[2,3]])\n",
    "np.resize(a,(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "49544b11-1bc1-4b9a-9fbc-17ae1eba59be",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3]])"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.resize(a,(1,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "8271d886-7b2d-41ce-99a4-080861f9c187",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [0, 1, 2, 3]])"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.resize(a,(2,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb8384a7-0868-462b-9266-097ea19e942b",
   "metadata": {},
   "source": [
    "### numpy.trim_zeros(filt, trim='fb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "8da55b93-a2c7-4cad-804d-e3463cefe276",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 0, 2, 1])"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))\n",
    "np.trim_zeros(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "8695808f-69d2-4c4d-beae-f889645b2266",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 1, 2, 3, 0, 2, 1])"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.trim_zeros(a, 'b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "52612b76-b99e-45e5-87b0-ab148d2c524e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.trim_zeros([0, 1, 2, 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38cc8895-cbe6-4768-9c16-1f949a6d2b30",
   "metadata": {},
   "source": [
    "### numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None, *, equal_nan=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "a602a417-a478-4de6-9b6d-a2e0b0cfea17",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique([1, 1, 2, 2, 3, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "86b54761-f917-4584-af86-5976f0d3f19b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 1], [2, 3]])\n",
    "np.unique(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "02c01878-651e-4703-aa1b-55e800c8b61e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [2, 3, 4]])"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])\n",
    "np.unique(a, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "id": "18b864ad-e219-4692-aa6b-986c12e84df9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array(['a', 'b', 'c'], dtype='<U1'), array([0, 1, 3]))"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array(['a', 'b', 'b', 'c', 'a'])\n",
    "u, indices = np.unique(a, return_index=True)\n",
    "u, indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "id": "2021916e-6ace-4838-ae51-2eaae525ce15",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['a', 'b', 'c'], dtype='<U1')"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[indices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "73a6cdaa-2a30-46f1-a9b7-df65598b0331",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4, 6]), array([0, 1, 4, 3, 1, 2, 1]))"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 6, 4, 2, 3, 2])\n",
    "u, indices = np.unique(a, return_inverse=True)\n",
    "u, indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "a8f9444b-c840-4dbd-b998-883b964cce97",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 6, 4, 2, 3, 2])"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u[indices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "ad2bcb89-3179-4776-a919-1be45c529b2d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4, 6]), array([1, 3, 1, 1, 1]))"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 6, 4, 2, 3, 2])\n",
    "values, counts = np.unique(a, return_counts=True)\n",
    "values, counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "9b5edbf7-67c0-4e0a-b37c-6e1e2e0452bd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 2, 2, 3, 4, 6])"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(values, counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6504e85-337a-4f01-a9c0-2a55a91671a3",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 重新排列元素\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "flip(m[, axis])|沿给定轴颠倒数组中元素的顺序。\n",
    "fliplr(m)|左右翻转数组。\n",
    "flipud(m)|上下翻转阵列。\n",
    "reshape(a, newshape[, order])|在不更改数据的情况下为数组赋予新的形状。\n",
    "roll(a, shift[, axis])|沿给定轴滚动数组元素。\n",
    "rot90(m[, k, axes])|在轴指定的平面中将阵列旋转90度。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b694eab-18c6-44f0-8c81-fadafb6f52ef",
   "metadata": {},
   "source": [
    "### numpy.flip(m, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "b5646f4c-9416-4419-9341-389f92d9a079",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[[0, 1],\n",
       "         [2, 3]],\n",
       " \n",
       "        [[4, 5],\n",
       "         [6, 7]]]),\n",
       " array([[[4, 5],\n",
       "         [6, 7]],\n",
       " \n",
       "        [[0, 1],\n",
       "         [2, 3]]]))"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.arange(8).reshape((2,2,2))\n",
    "A, np.flip(A, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "7ea82d2f-c5a5-4405-86d6-8fed5b30ba3e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[2, 3],\n",
       "        [0, 1]],\n",
       "\n",
       "       [[6, 7],\n",
       "        [4, 5]]])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flip(A, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "555ae5c7-299b-4c06-9a07-a83d328b5e7f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[7, 6],\n",
       "        [5, 4]],\n",
       "\n",
       "       [[3, 2],\n",
       "        [1, 0]]])"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flip(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "id": "446eb14f-9718-4745-9302-6a8506c0e98a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[5, 4],\n",
       "        [7, 6]],\n",
       "\n",
       "       [[1, 0],\n",
       "        [3, 2]]])"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flip(A, (0, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "62120a8d-da09-499a-97a7-bb27fc7dfbd4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.random.randn(3,4,5)\n",
    "np.all(np.flip(A,2) == A[:,:,::-1,...])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a31a130-f9b7-4b18-b785-5249a30d2998",
   "metadata": {},
   "source": [
    "### numpy.fliplr(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "93d75bf1-ab0a-435b-9a17-118c07a22f5a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 0., 0.],\n",
       "        [0., 2., 0.],\n",
       "        [0., 0., 3.]]),\n",
       " array([[0., 0., 1.],\n",
       "        [0., 2., 0.],\n",
       "        [3., 0., 0.]]))"
      ]
     },
     "execution_count": 197,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.diag([1.,2.,3.])\n",
    "A, np.fliplr(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "6ddc95d6-8299-474a-8be6-51e6b8067882",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.random.randn(2,3,5)\n",
    "np.all(np.fliplr(A) == A[:,::-1,...])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a6e156-e72e-48f4-969a-6e38c283eb82",
   "metadata": {},
   "source": [
    "### numpy.flipud(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "1d6d3b81-2d1d-4358-9a75-fdb80500bdb2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 0., 0.],\n",
       "        [0., 2., 0.],\n",
       "        [0., 0., 3.]]),\n",
       " array([[0., 0., 3.],\n",
       "        [0., 2., 0.],\n",
       "        [1., 0., 0.]]))"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.diag([1.0, 2, 3])\n",
    "A, np.flipud(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "1e4f9f90-e065-4e9f-a5bf-5f646f21195e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.random.randn(2,3,5)\n",
    "np.all(np.flipud(A) == A[::-1,...])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "id": "d95b2111-40c3-458c-ad5d-183fc5ad26a9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1])"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flipud([1,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac25bdba-dc34-4ff4-8d06-e03990eb67f5",
   "metadata": {},
   "source": [
    "### numpy.reshape(a, newshape, order='C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "id": "f161c0fa-0b6c-4a2c-a84d-b3ac18921ba6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5]])"
      ]
     },
     "execution_count": 202,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(6).reshape((3, 2))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "id": "6f09c2e8-a678-4182-949f-b246e196c8b2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, (2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "6084fdaa-6fbc-4fc2-b1fb-f0fd7b328faf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(np.ravel(a), (2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "2118c14d-aa9d-4ed6-86fd-b212d5bf3d4f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 4, 3],\n",
       "       [2, 1, 5]])"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, (2, 3), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "18107283-b42f-495b-b3f3-f62ce1cc9964",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 4, 3],\n",
       "       [2, 1, 5]])"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(np.ravel(a, order='F'), (2, 3), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "7d81ec59-4450-461b-a9ca-508befac968c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,6]])\n",
    "np.reshape(a, 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "a69c0495-05cf-4687-b0a5-53b33ffc66ec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 2, 5, 3, 6])"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, 6, order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "id": "dde9ad90-8832-4a01-bf96-1d2d1788e5af",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a, (3,-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "874e8b8e-4e97-4004-8b59-3d2379bf13c0",
   "metadata": {},
   "source": [
    "### numpy.roll(a, shift, axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "id": "67806ef6-4e03-4fc8-8cdb-44cb5fc3a026",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])"
      ]
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(10)\n",
    "np.roll(x, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "id": "22b95781-b50b-4689-85a5-7ea0976f1797",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x, -2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "2c9813b8-f07c-4839-828a-0c48114b9150",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = np.reshape(x, (2, 5))\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "38369583-1256-42a2-843f-2ce31d761628",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 0, 1, 2, 3],\n",
       "       [4, 5, 6, 7, 8]])"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "id": "93f24f8f-9d0d-4f34-9286-806e20c05f67",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5],\n",
       "       [6, 7, 8, 9, 0]])"
      ]
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "04a9501b-8ea6-4649-a99d-dc7703a0e461",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 7, 8, 9],\n",
       "       [0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, 1, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "id": "80031710-8d17-4089-b6fd-539caf7f08c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 7, 8, 9],\n",
       "       [0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, -1, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "id": "d26a0dfe-055e-45a2-a3f0-939905701009",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 1, 2, 3],\n",
       "       [9, 5, 6, 7, 8]])"
      ]
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, 1, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "06e8e1f9-110d-4e69-8744-f432d7ec9421",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 0],\n",
       "       [6, 7, 8, 9, 5]])"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, -1, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "id": "5b2d49f8-2e7b-41a2-b5f7-e2a8669da568",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 5, 6, 7, 8],\n",
       "       [4, 0, 1, 2, 3]])"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, (1, 1), axis=(1, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "b003fa3b-02ae-43f7-b892-593299bf065a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 9, 5, 6, 7],\n",
       "       [3, 4, 0, 1, 2]])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roll(x2, (2, 1), axis=(1, 0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fc97c61-341f-4abc-85b1-62c8e1059bfe",
   "metadata": {},
   "source": [
    "### numpy.rot90(m, k=1, axes=(0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "482147e8-23e8-453b-abed-e8f98f35c2e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = np.array([[1,2],[3,4]], int)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "8e29e276-daa8-496d-a90b-91ceac875f1b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [1, 3]])"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rot90(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "82097b21-ab10-49cc-895f-771651bdd022",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 3],\n",
       "       [2, 1]])"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rot90(m, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "id": "e5cc40e3-1856-4be0-9e10-4ebfed423fed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 3],\n",
       "        [0, 2]],\n",
       "\n",
       "       [[5, 7],\n",
       "        [4, 6]]])"
      ]
     },
     "execution_count": 224,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = np.arange(8).reshape((2,2,2))\n",
    "np.rot90(m, 1, (1,2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
